package com.netted.operationassistant.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.netted.operationassistant.R;
import com.netted.operationassistant.constant.Constant;
import com.netted.operationassistant.http.HttpRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OpenPortActivity extends BaseActivity {
    private TableLayout tableLayout;
    private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
    private final int MP = ViewGroup.LayoutParams.MATCH_PARENT;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_port);
        tableLayout = (TableLayout) findViewById(R.id.port_table_layout);
        getPort();
    }


    public List<JSONObject> getPort() {
        final ProgressDialog progressDialog = ProgressDialog.show(this,"Loading...", "读取中...", true, false);
        String url = Constant.HOST + "/ws/opt/getPortByCatbinetId";
        Map<String, String> params = new HashMap<>();
        params.put("catbinetId", sharedPreferencesHelper.getSharedPreference("id", "") + "");

        request.post(url, params, "post", new HttpRequest.Callback() {
            @Override
            public void onSuccess(String response) {
                progressDialog.dismiss();
                if (!StringUtils.isEmpty(response)){
                    JSONObject dataJson = JSONObject.parseObject(response);
                    String flag = dataJson.getString("flag");
                    if ("success".equals(flag)){
                        JSONArray datas = JSONArray.parseArray(dataJson.getString("data"));
                        if (datas != null && datas.size() > 0){
                            showPort(datas);
                        }

                    }

                }

            }

            @Override
            public void onError(VolleyError error) {
                progressDialog.dismiss();
                ToastUtils.showLong("网络异常");
            }
        });
        return null;
    }


    public void showPort(JSONArray datas) {
        int count = datas.size();
        int col = 5;
        int row = count % col == 0 ? count / col : count / col + 1;
        //清除表格所有行
        tableLayout.removeAllViews();
        //全部列自动填充空白处
        tableLayout.setStretchAllColumns(true);
        LinearLayout.LayoutParams lp = new TableRow.LayoutParams(-1,-1);
        lp.setMargins(1,1,1,1);
        lp.height = 80;
        int c = 0;
        for (int i = 0; i < row; i++) {
            TableRow tableRow = new TableRow(OpenPortActivity.this);
            tableRow.setGravity(Gravity.CENTER);
            for (int j = 0; j < col ; j ++){

                final int num = c;
                Button btn = new Button(OpenPortActivity.this);
                btn.setTextSize(30);
                btn.setLayoutParams(lp);
                btn.setBackgroundResource(R.drawable.public_button_selector);
                if (c < count){
                    btn.setText("(" + (c) + ")号机柜" );
                    btn.setTextColor(getResources().getColor(R.color.public_button_text_color_selector));
                    btn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ToastUtils.showLong("(" + (num) + ")号机柜已发送开柜指令" );
                            try {

                                openPort(num+"");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }else{
                    btn.setText("" );
                    tableRow.setEnabled(false);
                }
                c++;
                tableRow.addView(btn);
            }
            tableLayout.addView(tableRow, new TableLayout.LayoutParams(MP, WC, 1));
        }


    }

    public void exitActivity(View view){
        Intent intent = new Intent(this,AdminMenuActivity.class);
        startActivity(intent);
        this.finish();
    }


    public void openAll(View view){
        openPort("00","FF");
        openPort("01","FF");
        openPort("03","FF");
        ToastUtils.showLong("已发送开柜指令" );
    }
}
